<!--
 * @Author: XieQiu 雪球
 * @Date: 2021-05-31 15:07:49
 * @LastEditTime: 2022-02-12 14:13:05
 * @LastEditors: Please set LastEditors
 * @Description: 仓库管理页面
-->
<template>
  <BaseMainSlot>
    <template v-slot:btn-box>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        :has-edit="false"
        :has-save="false"
        :has-cancel="false"
        @add="add"
        @cancel="cancel('formData')"
        @edit="edit"
        @del="del"
        @save="save('formData')"
      />
    </template>
    <template v-slot:search-box>
      <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label">仓库类型名称:</div>
          <div class="input">
            <el-input v-model="warehouseTypeName" placeholder="请输入仓库类型名称" clearable />
          </div>
        </div>
      </SearchBox>
    </template>
    <template v-slot:table-box>
      <el-table
        ref="tableData"
        v-loading="loading"
        :data="tableData"
        stripe
        border
        height="88%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="50" />
        <el-table-column prop="warehouseTypeName" label="仓库类型" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column width="120" label="操作">
          <template v-if="scope.row.warehouseTypeId" slot-scope="scope">
            <el-button class="text-btn-cursor" type="text" size="small" @click="handleEdit(scope.row)"><i class="el-icon-edit-outline"></i> 修改</el-button>
            <el-button class="text-btn-cursor cursor-delete" type="text" size="small" @click="handleDelete(scope.row)"><i class="el-icon-delete"></i> 删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog :title="dialogTitle + '仓库类型信息'" :visible.sync="dialogFormVisible" :before-close="cancel">
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <el-form-item label="仓库类型名称:" prop="warehouseTypeName" class="w100p">
            <el-input
              v-model="formData.warehouseTypeName"
              placeholder="请输入仓库类型名称"
              clearable
            />
          </el-form-item>
          <el-form-item label="备注:" prop="remark" class="w100p">
            <el-input
              v-model="formData.remark"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4 }"
              clearable
            />
          </el-form-item>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">
            取 消
          </el-button>
          <el-button class="zh-button-two" type="primary" @click="save('formData')">
            保 存
          </el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import warehouseTypeManage from "@/api/basic-info-manage/warehouse-type-manage";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot";
import SearchBox from "@/components/SearchBox";
import { mymixin } from "@/utils/mixin";
export default {
  name: "",
  components: { ActionBar, Pagination, BaseMainSlot, SearchBox },
  mixins: [mymixin],
  data() {
    return {
      obj: {
        name: ""
      },
      // 仓库名称
      warehouseTypeName: "",
      // 是否能输入
      canInput: false,
      // 选中的数据项数组 ID组成
      warehouseTypeIdList: [],
      rules: {
        warehouseTypeName: [{ required: true, message: '请输入仓库类型名称', trigger: 'blur' }]
        // remark: [{ required: true, message: '请选择巡查线路', trigger: 'change' }]
      },
      dialogTitle: "",
      dialogFormVisible: false
    };
  },
  watch: {},
  async created() {
    // 获取表格数据
    await this.getTableDataList();
  },
  mounted() {},
  methods: {
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          warehouseTypeName: this.warehouseTypeName
        }
      };
      warehouseTypeManage.getWareHouseTypeList(params).then(res => {
        this.loading = false;
        let data = res.data;
        // console.log(data)
        // 赋值总条数
        if (data) {
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    add() {
      this.formData = {};
      this.openDialog('add');
    },
    handleEdit(row) {
      // TODO 点击行元素后 edit
      this.formData = JSON.parse(JSON.stringify(row));
      this.editActive = true;
      this.saveActive = false;
      this.cancelActive = true;
      this.canInput = false;
      this.openDialog('edit');
    },
    handleSelectionChange(list) {
      this.warehouseTypeIdList = [];
      this.warehouseTypeIdList = list.map(item => item.warehouseTypeId);
      if (this.warehouseTypeIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    delmessage() {
      warehouseTypeManage
        .delWareHouseTypeInfo(this.warehouseTypeIdList)
        .then(res => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.$refs.formData.resetFields();
            this.getTableDataList();
          } else {
            this.$message.warning(res.msg || '删除失败');
          }
        });
    },
    handleDelete(row) {
      warehouseTypeManage
        .delWareHouseTypeInfo([row.warehouseTypeId])
        .then(res => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.$refs.formData.resetFields();
            this.getTableDataList();
          } else {
            this.$message.warning(res.msg || '删除失败');
          }
        });
    },
    cancel() {
      // TODO 取消后 除了查看二维码外 input不可输入 add 1 其余 0
      this.getTableDataList();
      this.closeDialog();
    },
    save(formData) {
      this.$refs[formData].validate((valid) => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            // 编辑接口
            warehouseTypeManage.editWareHouseTypeInfo(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "修改成功！");
                this.closeDialog();
                this.$refs[formData].resetFields();
                this.getTableDataList();
              } else {
                this.$message.error(res.msg || "修改失败！");
              }
            });
          } else if (this.nowChanceStau === "add") {
            // 新增接口
            this.formData.organizationNo = sessionStorage.organizationNo
            warehouseTypeManage.addWareHouseTypeInfo(this.formData).then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "新增成功！");
                this.closeDialog();
                this.$refs[formData].resetFields();
                this.getTableDataList();
              } else {
                this.$message.error(res.msg || "新增失败！");
              }
            });
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === 'add' ? '新增' : '编辑'
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = '';
    }
  }
};
</script>

<style scoped lang="scss">
@import "~@/styles/variables.scss";
.patrol-point-manage {
  width: 100%;
  height: 100%;
  ::v-deep .el-form-item__content {
    display: flex;
    align-items: center;
  }
  ::v-deep .el-form-item__label {
    min-width: 160px;
    text-align: end;
  }
  .dialog {
    ::v-deep .el-dialog__title {
      font-weight: bold;
    }
    .qrCode {
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  .search-box {
    display: flex;
    align-items: center;
    margin: 10px 0;
    .label-input {
      display: flex;
      align-items: center;
      .label {
        width: 80px;
        margin-right: 30px;
        white-space: nowrap;
      }
    }
    .search {
      display: flex;
      align-items: center;
      img {
        height: 30px;
      }
    }
  }
  .common-layout {
    height: calc(100% - #{$actionBarHeight + 10});
    display: flex;
    .left-box {
      width: 50%;
      height: calc(100% - 10px);
      .table-box {
        height: 100%;
        position: relative;
        overflow-y: auto;
      }
    }
    .right-box {
      width: 49%;
      height: calc(100% - 80px);
      margin-left: 1%;
      border: 1px dashed $borderColor;
      border-radius: 10px;
      padding: 20px;
      .content {
        height: 100%;
        .form {
          overflow-y: auto;
          height: 100%;
          .el-form {
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
          }
          ::v-deep .el-form-item__label {
            min-width: 120px;
            text-align: end;
          }
          ::v-deep .el-input__inner {
            border-radius: 5px;
          }
          ::v-deep .el-textarea__inner {
            border-radius: 5px;
          }
          .map {
            width: 100%;
            height: 300px;
          }
        }
      }
    }
  }
}
</style>
